package editor.osx;

import adventure.placeable.PlaceableManager;
import adventure.placeable.PlaceableManagerEdit;
import adventure.placeable.trigger.Trigger;
import adventure.placeable.trigger.TriggerEdit;
import adventure.placeable.trigger.conditions.PlayerCondition;
import adventure.placeable.trigger.conditions.RoomCondition;
import adventure.placeable.trigger.conditions.StringCondition;
import adventure.placeable.trigger.conditions.TriggerCondition;
import adventure.placeable.trigger.results.TriggerResult;

/**
 *
 * @author Michael Hanns
 *
 */
public class DialogAddTriggerConditionView extends javax.swing.JDialog {

    private static final long serialVersionUID = 3177896795880161191L;
    private TriggerEdit t;
    private TriggerResult tr;
    private int position;
    private PlaceableManager p;
    private WITNAFrameView parent;

    /** Creates new form DialogAddTriggerConditionView */
    public DialogAddTriggerConditionView(java.awt.Frame parent,
            TriggerEdit t, TriggerResult tr, int position, PlaceableManager p) {
        super(parent, true);
        this.parent = (WITNAFrameView) parent;
        initComponents();
        this.t = t;
        this.tr = tr;
        this.position = position;
        this.p = p;
        updateFields();
    }

    private void updateFields() {
        if (userRadio.isSelected()) {
            updateUserInputFields(conditionCombo.getSelectedItem().toString());
        } else if (roomRadio.isSelected()) {
            updateRoomFields(conditionCombo.getSelectedItem().toString());
        } else if (playerRadio.isSelected()) {
            updatePlayerFields(conditionCombo.getSelectedItem().toString());
        }
    }

    private void updateUserInputFields(String condition) {
        if (condition.equals("EQUALS")) {
            idField.setEnabled(false);
            quantityField.setEnabled(false);
            textField.setEnabled(true);
        } else if (condition.equals("PREVIOUSFAILED")) {
            idField.setEnabled(false);
            quantityField.setEnabled(false);
            textField.setEnabled(false);
        }
        if (idField.isEnabled()) {
            idField.setText("0");
        } else {
            idField.setText("");
        }
        if (quantityField.isEnabled()) {
            quantityField.setText("0");
        } else {
            quantityField.setText("");
        }

        textField.setText("");

    }

    private void updateRoomFields(String condition) {
        if (condition.equals("HASITEM")) {
            idField.setEnabled(true);
            quantityField.setEnabled(true);
            textField.setEnabled(false);
        } else if (condition.equals("HASCURRENCYOVER")) {
            idField.setEnabled(false);
            quantityField.setEnabled(true);
            textField.setEnabled(false);
        } else if (condition.equals("HASCURRENCYATLEAST")) {
            idField.setEnabled(false);
            quantityField.setEnabled(true);
            textField.setEnabled(false);
        } else if (condition.equals("HASCURRENCYUNDER")) {
            idField.setEnabled(false);
            quantityField.setEnabled(true);
            textField.setEnabled(false);
        } else if (condition.equals("HASCURRENCYATMOST")) {
            idField.setEnabled(false);
            quantityField.setEnabled(true);
            textField.setEnabled(false);
        } else if (condition.equals("HASMOBILE")) {
            idField.setEnabled(true);
            quantityField.setEnabled(true);
            textField.setEnabled(false);
        } else if (condition.equals("HASTRIGGER")) {
            idField.setEnabled(true);
            quantityField.setEnabled(true);
            textField.setEnabled(false);
        } else if (condition.equals("TITLEEQUALS")) {
            idField.setEnabled(false);
            quantityField.setEnabled(false);
            textField.setEnabled(true);
        } else if (condition.equals("PREVIOUSFAILED")) {
            idField.setEnabled(false);
            quantityField.setEnabled(false);
            textField.setEnabled(false);
        }
        if (idField.isEnabled()) {
            idField.setText("0");
        } else {
            idField.setText("");
        }
        if (quantityField.isEnabled()) {
            quantityField.setText("0");
        } else {
            quantityField.setText("");
        }

        textField.setText("");
    }

    private void updatePlayerFields(String condition) {
        if (condition.equals("HASITEM")) {
            idField.setEnabled(true);
            quantityField.setEnabled(true);
            textField.setEnabled(false);
        } else if (condition.equals("KNOWSSKILL") || condition.equals("ISVOCATION")) {
            idField.setEnabled(true);
            quantityField.setEnabled(false);
            textField.setEnabled(false);
        } else if (condition.equals("ISLEVEL") || condition.equals("HASLOGENTRY")
                || condition.equals("HASNOTLOGENTRY") || condition.equals("COMPLETEDLOGENTRY")
                || condition.equals("INCOMPLETELOGENTRY")
                || condition.equals("HASHEALTH") || condition.equals("HASSPECIAL")
                || condition.equals("HASATTACK") || condition.equals("HASDEFENCE")
                || condition.equals("HASINTEL") || condition.equals("HASSPEED")
                || condition.equals("HASLUCK")) {
            idField.setEnabled(true);
            quantityField.setEnabled(false);
            textField.setEnabled(false);
        } else if (condition.equals("HASCURRENCYUNDER")) {
            idField.setEnabled(false);
            quantityField.setEnabled(true);
            textField.setEnabled(false);
        } else if (condition.equals("HASCURRENCYATMOST")) {
            idField.setEnabled(false);
            quantityField.setEnabled(true);
            textField.setEnabled(false);
        } else if (condition.equals("HASCURRENCYOVER")) {
            idField.setEnabled(false);
            quantityField.setEnabled(true);
            textField.setEnabled(false);
        } else if (condition.equals("HASCURRENCYATLEAST")) {
            idField.setEnabled(false);
            quantityField.setEnabled(true);
            textField.setEnabled(false);
        } else if (condition.equals("NAMEEQUALS")) {
            idField.setEnabled(false);
            quantityField.setEnabled(false);
            textField.setEnabled(true);
        } else if (condition.startsWith("STATCHECK") || condition.startsWith("FAILSTATCHECK")) {
            idField.setEnabled(true);
            quantityField.setEnabled(false);
            textField.setEnabled(false);
        } else if (condition.equals("PREVIOUSFAILED")) {
            idField.setEnabled(false);
            quantityField.setEnabled(false);
            textField.setEnabled(false);
        }
        if (idField.isEnabled()) {
            idField.setText("0");
        } else {
            idField.setText("");
        }
        if (quantityField.isEnabled()) {
            quantityField.setText("0");
        } else {
            quantityField.setText("");
        }

        textField.setText("");
    }
    
    private void addCondition() {
        if (userRadio.isSelected()) {
            addUserInputCondition();
        } else if (roomRadio.isSelected()) {
            addRoomCondition();
        } else if (playerRadio.isSelected()) {
            addPlayerCondition();
        }
        t.editResult(tr, position);
        ((PlaceableManagerEdit) p).editTrigger((Trigger) t);
    }

    private void addUserInputCondition() {
        TriggerCondition inputCond =
                new StringCondition(conditionCombo.getSelectedItem().toString(),
                textField.getText(), notBox.isSelected());
        tr.addCondition(inputCond);
        parent.setUnsavedChanges(true);
    }

    private void addPlayerCondition() {
        TriggerCondition inputCond =
                new PlayerCondition(conditionCombo.getSelectedItem().toString(),
                parseInt(idField.getText()), parseInt(quantityField.getText()),
                textField.getText(), notBox.isSelected());
        parent.setUnsavedChanges(true);
        tr.addCondition(inputCond);
    }

    private void addRoomCondition() {
        TriggerCondition inputCond =
                new RoomCondition(conditionCombo.getSelectedItem().toString(),
                parseInt(idField.getText()), parseInt(quantityField.getText()),
                textField.getText(), notBox.isSelected());
        parent.setUnsavedChanges(true);
        tr.addCondition(inputCond);
    }

    private int parseInt(String text) {
        try {
            int i = Integer.parseInt(text);
            return i;
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    private void updateConditionSubtype() {
        javax.swing.DefaultComboBoxModel model = new javax.swing.DefaultComboBoxModel();
        if (userRadio.isSelected()) {
            model.addElement("EQUALS");
            model.addElement("PREVIOUSFAILED");
        } else if (roomRadio.isSelected()) {
            model.addElement("HASITEM");
            model.addElement("HASMOBILE");
            model.addElement("HASTRIGGER");
            model.addElement("HASCURRENCYOVER");
            model.addElement("HASCURRENCYATLEAST");
            model.addElement("HASCURRENCYUNDER");
            model.addElement("HASCURRENCYATMOST");
            model.addElement("TITLEEQUALS");
            model.addElement("PREVIOUSFAILED");
            conditionCombo.setModel(model);
            updateFields();
        } else if (playerRadio.isSelected()) {
            model.addElement("HASITEM");
            model.addElement("HASCURRENCYOVER");
            model.addElement("HASCURRENCYATLEAST");
            model.addElement("HASCURRENCYUNDER");
            model.addElement("HASCURRENCYATMOST");
            model.addElement("HASHEALTH");
            model.addElement("HASSPECIAL");
            model.addElement("HASATTACK");
            model.addElement("HASDEFENCE");
            model.addElement("HASINTEL");
            model.addElement("HASSPEED");
            model.addElement("HASLUCK");
            model.addElement("STATCHECKATTACK");
            model.addElement("STATCHECKDEFENCE");
            model.addElement("STATCHECKINTEL");
            model.addElement("STATCHECKSPEED");
            model.addElement("STATCHECKLUCK");
            model.addElement("KNOWSSKILL");
            model.addElement("HASCURRENCY");
            model.addElement("HASLOGENTRY");
            model.addElement("HASNOTLOGENTRY");
            model.addElement("COMPLETEDLOGENTRY");
            model.addElement("INCOMPLETELOGENTRY");
            model.addElement("NAMEEQUALS");
            model.addElement("PREVIOUSFAILED");
        }
        conditionCombo.setModel(model);
        updateFields();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        conditionGroup = new javax.swing.ButtonGroup();
        conditionLabel = new javax.swing.JLabel();
        userRadio = new javax.swing.JRadioButton();
        roomRadio = new javax.swing.JRadioButton();
        playerRadio = new javax.swing.JRadioButton();
        quantityField = new javax.swing.JFormattedTextField();
        idLabel4 = new javax.swing.JLabel();
        idField = new javax.swing.JFormattedTextField();
        idLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        textField = new javax.swing.JFormattedTextField();
        conditionCombo = new javax.swing.JComboBox();
        addConditionButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        notBox = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Add Trigger Condition..");
        setLocationByPlatform(true);
        setResizable(false);

        conditionLabel.setText("Conditions:");

        conditionGroup.add(userRadio);
        userRadio.setSelected(true);
        userRadio.setText("User Input");
        userRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                userRadioActionPerformed(evt);
            }
        });

        conditionGroup.add(roomRadio);
        roomRadio.setText("Room");
        roomRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                roomRadioActionPerformed(evt);
            }
        });

        conditionGroup.add(playerRadio);
        playerRadio.setText("Player");
        playerRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                playerRadioActionPerformed(evt);
            }
        });

        quantityField.setColumns(3);
        quantityField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter()));
        quantityField.setEnabled(false);

        idLabel4.setText("Quantity:");

        idField.setColumns(3);
        idField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter()));
        idField.setEnabled(false);

        idLabel.setText("Int/ID:");

        jLabel1.setText("Text Condition:");

        textField.setColumns(10);

        conditionCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "EQUALS", "PREVIOUSFAILED" }));
        conditionCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                conditionComboActionPerformed(evt);
            }
        });

        addConditionButton.setText("Add Condition");
        addConditionButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addConditionButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        notBox.setText("Not");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(roomRadio)
                                .addGap(23, 23, 23))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(conditionLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(playerRadio)
                            .addComponent(userRadio))
                        .addGap(18, 18, 18)
                        .addComponent(notBox)
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(conditionCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(textField))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(idLabel)
                            .addComponent(idField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(idLabel4)
                            .addComponent(quantityField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(cancelButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(addConditionButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(userRadio)
                            .addComponent(conditionLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(roomRadio)
                            .addComponent(playerRadio))
                        .addGap(18, 18, 18))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(conditionCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(notBox))
                        .addGap(34, 34, 34)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(idLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(quantityField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(idField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addComponent(idLabel))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(textField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addConditionButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void conditionComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_conditionComboActionPerformed
        updateFields();
    }//GEN-LAST:event_conditionComboActionPerformed

    private void userRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_userRadioActionPerformed
        updateConditionSubtype();
    }//GEN-LAST:event_userRadioActionPerformed

    private void roomRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_roomRadioActionPerformed
        updateConditionSubtype();
    }//GEN-LAST:event_roomRadioActionPerformed

    private void playerRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_playerRadioActionPerformed
        updateConditionSubtype();
    }//GEN-LAST:event_playerRadioActionPerformed

    private void addConditionButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addConditionButtonActionPerformed
        addCondition();
        dispose();
    }//GEN-LAST:event_addConditionButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addConditionButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JComboBox conditionCombo;
    private javax.swing.ButtonGroup conditionGroup;
    private javax.swing.JLabel conditionLabel;
    private javax.swing.JFormattedTextField idField;
    private javax.swing.JLabel idLabel;
    private javax.swing.JLabel idLabel4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JCheckBox notBox;
    private javax.swing.JRadioButton playerRadio;
    private javax.swing.JFormattedTextField quantityField;
    private javax.swing.JRadioButton roomRadio;
    private javax.swing.JFormattedTextField textField;
    private javax.swing.JRadioButton userRadio;
    // End of variables declaration//GEN-END:variables
}
